﻿using UnityEngine;
using System.Collections;

public class WallScript : MonoBehaviour {

	private GameObject counter;
	private PointScript script;

	private int id = -1;

	// Use this for initialization
	void Start () {
		counter = GameObject.FindGameObjectWithTag ("PointCounter");
		script = (PointScript)counter.GetComponent ("PointScript");
		rigidbody2D.velocity = new Vector3 (-1, 0, 0) * (2.0f*getSpeedModifyer());
		//Destroy(gameObject, 10);
	}

	void Update(){
		if (transform.position.x < -6) {
			Destroy(gameObject);
		}
	}

	void OnTriggerEnter2D(Collider2D other) {
		PlayerScript pScript = (PlayerScript)other.gameObject.GetComponent ("PlayerScript");
		WallScript wScript = (WallScript)gameObject.GetComponent ("WallScript");
		if (transform.tag == "jWall" && other.tag == "Player") {
			if (!pScript.isInAir ()) {
				DontDestroyOnLoad (counter);
				Application.LoadLevel ("GameOver");
			}
		} else if (transform.tag == "wall" && other.tag == "Player") {
			DontDestroyOnLoad (counter);
			Application.LoadLevel ("GameOver");
		} else if(transform.tag == "detector" && other.tag == "Player"){
			if(pScript.prevID != wScript.getId()) {
				script.addPoint ();
				pScript.prevID = wScript.getId();
			}
		}
	}

	float getSpeedModifyer(){
		return (script.getCurrentPoints()/10.0f)+1.0f;
	}

	public void setId(int newId){
		id = newId;
	}

	public int getId(){
		return id;
	}
}
